package com.sinvo.oa.exception;

import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description 异常全局处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        e.printStackTrace();
        return R.fail();
    }

    /**
     * 处理自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public R error(CustomException e) {
        return R.fail(e);
    }

    /**
     * 处理spring security AccessDeniedException异常
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public R error() throws AccessDeniedException {
        return R.fail(ResultCode.PERMISSION);
    }
}
